// The Vue build version to load with the `import` command
// (runtime-only or standalone) has been set in webpack.base.conf with an alias.
import Vue from 'vue'
import App from './App'
import router from './router'
import Vant from 'vant'
import 'vant/lib/index.css'
import VConsole from 'vconsole'
import wxShare from '@/utils/wechat/wxShare'
import '@/assets/css/base.less'

Vue.use(Vant)
Vue.config.productionTip = false

// 非线上环境 开启调试工具
if (process.env.NODE_ENV !== 'production') {
  let vConsole
  if (!vConsole) vConsole = new VConsole()
}

// 本地开发相关配置
if (process.env.NODE_ENV === 'development') {
}

router.beforeEach((to, from, next) => {
  // 处理缓存页面 - 如果是缓存页面  则记录高度
  if (from.meta.keepAlive) from.meta.savedPosition = document.documentElement.scrollTop || document.body.scrollTop

  // 配置本地开发配置
  if (process.env.NODE_ENV === 'development') {
    let openId = 'odrzB0i8JazndXGTFSbPokaFC8tg'
    sessionStorage.setItem('openId', openId)
    sessionStorage.setItem('token', 1)
  }

  // 处理微信授权 - 以openId为例
  if (to.query.openId) {
    if(!sessionStorage.setItem('openId') && sessionStorage.getItem('token')==1){
      sessionStorage.setItem('openId', to.query.openId)
    }
  }
  if (!sessionStorage.getItem('openId')) {
    sessionStorage.setItem('token', 1)
    // 未获取到openId时 跳转至后台提供的重定向地址
    window.location.href = process.env.WX_AUTH + '?url=' + window.location.href
    next(false)
  } else {
    next()
  }
})

router.afterEach((to, from) => {
  // 处理页面title
  if (to.meta.title) {
    document.title = to.meta.title
  }

  // 处理微信分享 - 分享的标题为meta中的shareTitle 描述为meta中的shareDesc  【Tips 需要跟进一步的自定义需要在单独的view中设置】
  let shareLink = to.meta.allowShare ? window.location.href : window.location.origin + router.history.base
  if (process.env.NODE_ENV !== 'development') wxShare(to.meta.shareTitle, to.meta.shareDesc, shareLink)
})

/* eslint-disable no-new */
new Vue({
  el: '#app',
  router,
  components: { App },
  template: '<App/>'
})
